package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;



/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    @ApiOperation("新增用户")
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工:{}",employeeDTO);
        employeeService.save(employeeDTO);

        return Result.success();
    }

    /**
     * 员工分页查询
     */

    @GetMapping("/page")
    @ApiOperation("用户分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询参数:{}",employeePageQueryDTO);
        PageResult pageResult=employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);


    }
    /**
     * 员工启用禁用功能
     */

    @PostMapping("/status/{status}")
    @ApiOperation("员工启用禁用功能")
    public Result startOtStop(@PathVariable Integer status,Long id){
        log.info("员工启用禁用功能：{},{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }
    /**
     * 根据id查询员工
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Employee> getById(@PathVariable Long id){
        Employee employee=employeeService.getById(id);
        log.info("查询到的员工信息,{}",employee);
        return Result.success(employee);
    }
    /**
     * 编辑员工信息
     */
    @PutMapping
    @ApiOperation("编辑员工信息功能")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息,{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
    /**
     * 编辑员工信息
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改员工密码")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("修改员工密码,{}",passwordEditDTO);
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        Double sum=0.00;
        DecimalFormat format = new DecimalFormat("#0.00");
        for (int i = 0; i < m; i++) {
            Double common= Double.valueOf(n);
            Double SSR=Double.valueOf((m - i));
            Double expectation = getExpectation(common,SSR);
            sum+=expectation;
        }
        sum= Double.valueOf(format.format(sum));
        System.out.println(sum);

    }
    public static Double getExpectation(Double common,Double SSR){
        DecimalFormat format = new DecimalFormat("#0.00");
        Double result= 1.0/Double.valueOf(SSR/(SSR+common));
        return Double.valueOf(format.format(result+1));
    }


}
